<?php
/**
 * @ClassName: Product
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-13 11:37
 * @Version: V1.0.0
 */

namespace app\api\controller\merchant;

use app\api\controller\Api;
use app\common\logic\GoodsLogic;
use app\common\model;

class Product extends Api
{
    /**
     * 品牌列表
     * @return \think\response\Json
     */
    function getBrandList()
    {
        $shop_type = input('shop_type', 2);

        $list = model\ShopBrand::where('shop_type', $shop_type)->where('is_show', 1)->select();

        return json(['code' => 1, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 分类列表
     * @return \think\response\Json
     */
    function getCategoryList()
    {
        $shop_type = input('shop_type', 2);

        $list = model\ShopCategory::where('shop_type', $shop_type)->where('is_show', 1)->select();

        return json(['code' => 1, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 单位列表
     * @return \think\response\Json
     */
    function getUnitList()
    {
        $list = model\ShopGoodsUnit::where('status', 1)->where('is_show', 1)->select();

        return json(['code' => 1, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 商品列表
     * @return \think\response\Json
     */
    function goodsList()
    {
        $keyword = input('keyword', '');
        $store_id = input('store_id', 0);
        $category_id = input('category_id', 0);
        $status = input('status', -1);
        $on_sale = input('on_sale', -1);
        $shop_type = input('shop_type', 2);
        $current = input('current', 0);
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $where = [];
        $where[] = ['is_show', '=', 1];
        $where[] = ['shop_type', '=', $shop_type];


        if ($keyword) {
            $where[] = ['name', 'like', '%' . $keyword . '%'];
        }
        if ($store_id) {
            $where[] = ['store_id', '=', $store_id];
        }
        if ($category_id) {
            $where[] = ['category_id', '=', $category_id];
        }

        if ($status != -1) {
            $where[] = ['status', '=', $status];
        }

        if ($on_sale != -1) {
            $where[] = ['on_sale', '=', $on_sale];
        }

        switch ($current) {
            case 1:
                // 待审核
                $where[] = ['status', '=', 0];
                break;
            case 2:
                // 已通过
                $where[] = ['status', '=', 1];
                break;
            case 3:
                // 被驳回
                $where[] = ['status', '=', 2];
                break;
            case 4:
                // 售卖中
                $where[] = ['status', '=', 1];
                $where[] = ['on_sale', '=', 1];
                break;
            case 5:
                // 已下架
                $where[] = ['status', '=', 1];
                $where[] = ['on_sale', '=', 0];
                break;
            default:
                // 全部
                break;
        }

        $goodsList = model\ShopGoods::where($where)
            ->order('create_time desc')
            ->limit($skip, $limit)
            ->select();

        foreach ($goodsList as &$goods) {
            $goods = (new GoodsLogic())->dataConversion($goods);
        }

        $result = [
            'data' => $goodsList,
            'is_more' => count($goodsList) >= $limit,
        ];

        return json(['code' => 1, 'data' => $result]);
    }

    /**
     * 保存商品
     * @return \think\response\Json
     */
    function save()
    {
        $data = input();
//        var_dump($data);

        $mer_config = web_config('merchant_config');

        if ($data['shop_type'] == 1) {
            // 联盟商品
            $goods = [
                'name' => $data['name'],
                'brand_id' => $data['brand_id'],
                'category_id' => $data['category_id'],
                'sale_price' => $data['sale_price'],
                'market_price' => $data['sale_price'],
                'cost_price' => $data['cost_price'],
                'delivery_price' => $data['delivery_price'],
                'delivery_type' => $data['delivery_type'],
                'stock' => $data['stock'],
                'unit_id' => $data['unit_id'],
                'invoice_type' => $data['invoice_type'],
                'invoice_ratio' => $data['invoice_ratio'],
                'description' => $data['description'],
                'logo' => $data['logo'],
                'images' => $data['images'],
                'shop_type' => $data['shop_type'],
                'store_id' => $data['store_id'],
                'admin_group_id' => $data['store_id'],
                'for_certs' => $data['for_certs'],
                'for_goods' => $data['for_goods'],
                'copy_id' => $data['copy_id'],
                'cost_price_points' => $mer_config['view_brand_supply_price'],
                'goods_type' => (new GoodsLogic())->getGoodsType($data['category_id']),
                'content' => $this->setContent($data),
                'on_sale' => 0,
                'status' => 0,
            ];
        } else {
            // 水站商品
            $goods = [
                'name' => $data['name'],
                'brand_id' => $data['brand_id'],
                'category_id' => $data['category_id'],
                'sale_price' => $data['sale_price'],
                'market_price' => $data['sale_price'],
                'cost_price' => $data['cost_price'],
                'delivery_price' => $data['delivery_price'],
                'delivery_type' => $data['delivery_type'],
                'stock' => $data['stock'],
                'unit_id' => $data['unit_id'],
                'shelf_life_unopened' => $data['shelf_life_unopened'],
                'shelf_life_opened' => $data['shelf_life_opened'],
                'delivery_time' => $data['delivery_time'],
                'invoice_type' => $data['invoice_type'],
                'invoice_ratio' => $data['invoice_ratio'],
                'description' => $data['description'],
                'logo' => $data['logo'],
                'images' => $data['images'],
                'shop_type' => $data['shop_type'],
                'store_id' => $data['store_id'],
                'admin_group_id' => $data['store_id'],
                'for_certs' => $data['for_certs'],
                'for_goods' => $data['for_goods'],
                'copy_id' => $data['copy_id'],
                'cost_price_points' => $mer_config['view_brand_supply_price'],
                'goods_type' => (new GoodsLogic())->getGoodsType($data['category_id']),
                'content' => $this->setContent($data),
                'on_sale' => 0,
                'status' => 0,
            ];
        }
        /*var_dump($goods);
        die();*/

        if (isset($data['id']) && $data['id']) {
            // 编辑
            $goods['status'] = 0;//审核状态-0待审核-1已通过-2已驳回
            $goods['update_time'] = date_now();
            $res = model\ShopGoods::where('id', $data['id'])->update($goods);
            if ($res) {
                $goods_id = $data['id'];
                $msg = '修改';
            } else {
                $goods_id = 0;
                $msg = '修改';
            }
        } else {
            // 新增
            $goods['create_time'] = date_now();
            $goods['update_time'] = date_now();
            $goods_id = model\ShopGoods::insertGetId($goods);
            $msg = '新增';
        }

        if (!$goods_id) {
            return json(['code' => 0, 'msg' => $msg . '失败']);
        }

        return json(['code' => 1, 'msg' => $msg . '成功', 'data' => $goods_id]);
    }

    /**
     * 组装商品详情
     * @param $data
     * @return string
     */
    private function setContent($data)
    {
        $content = $data['description'];
        $certs_ids = stringToArray($data['for_certs']);

        // 产品详情图
        $product_images = stringToArray($data['images']);
        foreach ($product_images as $item) {
            $content .= '<img src="' . $item . '">';
        }

        // 产品资质
        foreach ($certs_ids as $cert_id) {
            $cert = model\StoreCredential::where('id', $cert_id)->find();
            if ($cert) {
                $content .= "<p>" . model\StoreCredential::$certificate_types[$cert['type']] . "</p>";
                $images = stringToArray($cert['certificate_file']);
                foreach ($images as $image) {
                    $content .= '<img src="' . $image . '">';
                }
            }
        }

        return $content;
    }

}